package com.kr.redeye.member;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONSerializer;

import org.apache.commons.lang.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kr.redeye.dto.MemberTableDto;
import com.kr.redeye.util.HttpResponse;



/**
 * 기능 컨트롤러
 * <pre>
 * 
 * </pre>
 *
 * <pre>
 * <b>History:</b>
 *    강수, 1.0, 2013.9.04 초기작성
 * </pre>
 *
 * @author 최종 수정자
 * @version 1.0, 2013.9.04 메서드 추가함
 * @see    None
 */
@Controller
public class MemberController {
	
	private HttpSession loginSession = null;
	private static final String DEFAULT_REQUEST_MAPPING = "/member.do";
	private static final Logger logger = LoggerFactory.getLogger(MemberController.class);
	
	/** service */
	@Resource(name = "memberService")
	private MemberService memberService;
	

	/**
	 * 회원가입페이지 호출
	 *
	 * @param HttpServletRequest request, 리퀘스트 
	 * @param HttpServletResponse response , 리스폰스
	 * @return ModelAndView, jsp로 페이지 포워드 및 오브젝트 설정
	 * @exception 
	 */
	@RequestMapping(value = {DEFAULT_REQUEST_MAPPING}, params={"action=memberAddForm"})
	public ModelAndView newaccountForm(HttpServletRequest request, HttpServletResponse response) {
		logger.info("action=memberAddForm ");
		ModelAndView mv = new ModelAndView();
		mv.setViewName("member/new_account");
		return mv;
	}
	

	
	/**
	 * 회원가입check 유무 확인 후 가입처리
	 *
	 * @param HttpServletRequest request, 리퀘스트 
	 * @param HttpServletResponse response , 리스폰스
	 * @param MemberTableDto param , 멤버 테이블 dto
	 * @return int succ, 추가된 db row개수 반환
	 * @exception 
	 */
	@RequestMapping(value = {DEFAULT_REQUEST_MAPPING}, params={"action=insert"})
	public void insert(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("param") MemberTableDto param) {
		logger.info("action=insert "+ param.toString(ToStringStyle.MULTI_LINE_STYLE));
		int succ = 0;
		succ = memberService.insert(param);
		HttpResponse.sendMessage(response, Integer.toString(succ));

	}

	/**
	 * 로그인
	 *
	 * @param HttpServletRequest request, 리퀘스트 
	 * @param HttpServletResponse response , 리스폰스
	 * @param MemberTableDto param , 멤버 테이블 dto
	 * @return int succ, 아이디, 비밀번호 일치시 1 리턴 아닐땐 -1 리턴
	 * @exception 
	 */
	@RequestMapping(value = {DEFAULT_REQUEST_MAPPING}, params={"action=login"})
	public void login(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("param") MemberTableDto param) {
		logger.info("action=login "+ param.toString(ToStringStyle.MULTI_LINE_STYLE));
		int succ=0;
		MemberTableDto memberdto=null;
		memberdto=memberService.login(param);
		
		if(memberdto!=null){
			loginSession=request.getSession();
			loginSession.setAttribute("VE_MEMBER_ID", memberdto.getVE_MEMBER_ID());
			loginSession.setAttribute("USERNAME", memberdto.getUSERNAME());
			loginSession.setAttribute("STATE", memberdto.getSTATE());
			loginSession.setAttribute("VE_MEMBER_IDX", memberdto.getVE_MEMBER_IDX());
			
			if(memberdto.getVE_MEMBER_ID().equals("")){
				succ=-1;
			}
			else{
				succ=1;
			}
		}
		HttpResponse.sendMessage(response, Integer.toString(succ));
	}
	
	/**
	 * 기능데이터 수정
	 *
	 * @param HttpServletRequest request, 리퀘스트 
	 * @param HttpServletResponse response , 리스폰스
	 * @param FunctionTableDto param , 기능 테이블 dto
	 * @return int succ, 업데이트된 db row개수 반환
	 * @exception 
	 */
//	@RequestMapping(value = {DEFAULT_REQUEST_MAPPING}, params={"action=upd"})
//	public void upd(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("param") FunctionTableDto param) {
//		logger.info("action=upd "+ param.toString(ToStringStyle.MULTI_LINE_STYLE));
//		int succ = 0;
//		succ = FunctionService.upd(param);
//		HttpResponse.sendMessage(response, Integer.toString(succ));
//
//	}
//	
	/**
	 * 기능데이터 삭제
	 *
	 * @param HttpServletRequest request, 리퀘스트 
	 * @param HttpServletResponse response , 리스폰스
	 * @param FunctionTableDto param , 기능 테이블 dto
	 * @return int succ, 삭제된 db row개수 반환
	 * @exception 
	 */
//	@RequestMapping(value = {DEFAULT_REQUEST_MAPPING}, params={"action=del"})
//	public void del(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("param") FunctionTableDto param) {
//		logger.info("action=del "+ param.toString(ToStringStyle.MULTI_LINE_STYLE));
//		int succ = 0;
//		succ = FunctionService.del(param);
//		HttpResponse.sendMessage(response, Integer.toString(succ));
//
//	}
//	
	
}
